﻿package com.mdp.model 
{
	import org.papervision3d.materials.VideoStreamMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import flash.events.Event;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.objects.parsers.DAE;
	import com.mdp.model.Application;
	import flash.media.Video;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Prism 
	{
		static public var planeXML:XML;
		static public var prism:DAE;
		
		static public function parsePlaneXML(e:Event):void {
			planeXML = new XML(e.target.data);
		}
		
		static public function onCom( e:Event = null ) :void {
			prism = DAE(e.target);
			killIt(prism);
			prism.forceCoordSet = 10;
	   }
	   
	   static private function killIt(d3d:DisplayObject3D):void {
			tranceIt(d3d);
			
			for each(var child:DisplayObject3D in d3d.children) {
				killIt(child);
			}
		}
		
		static private function tranceIt(d3d:DisplayObject3D):void {
			//do some string stuff
			var xNode:String = planeXML..panel.(@name == d3d.name).@name.toString();
			
			if ( xNode == "" )
				return;

			var xType:String = planeXML..panel.(@name == d3d.name).@type.toString();
			trace("XXX" + xType);
			
			if (xType == "image") {
				var xU:String = planeXML..panel.(@name == d3d.name).@uri.toString();
				setIt( d3d, new BitmapFileMaterial( xU ) );
			} else if (xType == "movie") {
				
			} else if (xType == "live") {
				var xI:String = planeXML..panel.(@name == d3d.name).@uid.toString();
				var vid:Video = new Video();
				vid.attachNetStream(Application.stream.subscribeSteam(xI));
				var mMat:VideoStreamMaterial = new VideoStreamMaterial(vid, Application.stream.subscribeSteam(xI));
				mMat.doubleSided = true;
				setIt( d3d, mMat );
			} else if (xType == "webcam") {
				//setIt( d3d, planeMaterialCC );
			}
		}
	   
		static public function setIt( frame:Object, matti:MaterialObject3D ):void {
		   frame.material = matti;
		   	/*for each( var chi:Object in frame.children ) {
					chi.material = matti;
					chi.material.doubleSided = false;
					chi.material.smooth = true;
				    chi.material.precise = true;
			}*/
	   }
		
	}
	
}